package com.shujia.core

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Demo2Map {
  def main(args: Array[String]): Unit = {
    //创建Spark环境
    val conf = new SparkConf()
    conf.setMaster("local")
    conf.setAppName("map")

    val sc = new SparkContext(conf)

    //读取数据
    val linesRDD: RDD[String] = sc.textFile("spark/data/students.csv")

    /**
     * map:将数据一行一行传递给后面的函数，数据行数不变
     * mapValues： 只对value做处理，key不变
     */

    val idAndAgeRDD: RDD[(String, Int)] = linesRDD
      .map(line => {
        val split: Array[String] = line.split(",")
        (split(0), split(2).toInt)
      })

    idAndAgeRDD.foreach(println)

    val resultRDD: RDD[(String, Int)] = idAndAgeRDD
      .mapValues(age => age + 1)

    resultRDD.foreach(println)
  }

}
